import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution1 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param head ListNode类
     * @param m int整型
     * @param n int整型
     * @return ListNode类
     */
    public ListNode reverseBetween (ListNode head, int m, int n) {
        // write code here
        ListNode dum = new ListNode(0);
        ListNode cur = dum;
        cur.next = head;
        while(m > 1){
            m--;
            cur = cur.next;
            n--;
        }
        ListNode pre = null;
        ListNode c = cur.next;
        while(c != null && n>0){
            n--;
            ListNode Next = c.next;
            c.next = pre;
            pre = c;
            c = Next;
        }
        cur.next = pre;
        while(cur != null && cur.next != null){
            cur = cur.next;
        }
        cur.next = c;

        return dum.next;
    }
    // public ListNode reverse(ListNode head, int n){
    //     ListNode cur = head;
    //     ListNode pre = null;
    //     while(cur != null && n > 0){
    //         n--;
    //         ListNode Next = cur.next;
    //         cur.next = pre;
    //         pre = cur;
    //         cur = Next;
    //     }
    //     return pre;
    // }
}